<?php

namespace app\modules\weixin\controllers;

use app\common\services\HttpClient;
use app\common\services\StaticServices;
use app\models\Member;
use app\models\MemberIntegral;
use app\modules\weixin\controllers\common\BaseController;

class OauthController extends BaseController
{
    public function actionBind()
    {
        $scope = trim(\Yii::$app->request->get('scope', 'snsapi_base'));
        $member_id = intval(\Yii::$app->request->get('member_id'));
        $appid = \Yii::$app->params['weixin']['appid'];
        if ($member_id) {
            $redirect_url = urlencode(StaticServices::buildMUrl('/oauth/callback', ['member_id' => $member_id]));
        }
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appid}&redirect_uri={$redirect_url}&response_type=code&scope={$scope}&state=#wechat_redirect ";
        return $this->redirect($url);
    }

    public function actionCallback()
    {
        $code = \Yii::$app->request->get('code', '');
        $member_id = intval(\Yii::$app->request->get('member_id'));
        if (!$code) {
            return false;
        }
        if (!$member_id) {
            return false;
        }
        //通过code获取网页授权的access_token
        $appid = \Yii::$app->params['weixin']['appid'];
        $secret = \Yii::$app->params['weixin']['app_secret'];
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appid}&secret={$secret}&code={$code}&grant_type=authorization_code";
        $result = HttpClient::get($url);
        $ret = @json_decode($result, true);
        $ret_token = isset($ret['access_token']) ? $ret['access_token'] : '';
        if (!$ret_token) {
            return false;
        }
        $openid = isset($ret['openid']) ? $ret['openid'] : '';
        $scope = isset($ret['scope']) ? $ret['scope'] : '';

        //获取用户具体信息
        $weixin_user_info = [];
        if ($scope == 'snsapi_userinfo') {
            $url = "https://api.weixin.qq.com/sns/userinfo?access_token={$ret_token}&openid={$openid}&lang=zh_CN";
            $wechat_user_info = HttpClient::get($url);
            $weixin_user_info = @json_decode($wechat_user_info, true);
        }

//    \Yii::$app->response->format = Response::FORMAT_JSON;

        //判断用户是否存在
        $member_info = Member::find()->where(['id' => $member_id])->one();
        if ($member_info && $member_info->header_url == '') {
            $member_info->header_url = $weixin_user_info['headimgurl'];
            $member_info->member_integral += 2000;
            $member_info->update_time = time();
            if ($member_info->update(0)) {
                //添加用户积分信息
                $member_integral = new MemberIntegral();
                $member_integral->member_id = $member_info->id;
                $member_integral->integral = 2000;
                $member_integral->integral_source = '首次登录';
                $member_integral->update_time = time();
                $member_integral->create_time = time();
                $member_integral->save(0);
            }
        }

//        \Yii::$app->session->set('member_info', $member_info);
        //        return $this->redirect(UrlServices::buildMUrl('/lan-ting/index', ['share_id' => $share_id]));
        return $this->redirect("http://haitonghui.horsevision.cn/haitonghui/ownerWill/dist/index.html#/welcome?member_id={$member_id}");
    }
}